package Aspect;

import POJO.InputPOJO.UserPage;
import POJO.Model.ResponseModel;
import POJO.User;
import POJO.Vo.UserVo;
import Service.Impl.UserServiceImpl;
import Service.UserService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Aspect
@Component
public class UserAspect {

    @Resource
    ResponseModel responseModel;

    @Resource
    UserService userService;

    @Resource
    UserPage userPage;

    /*这个通知是用来对添加用户的参数合法性做验证的*/
    @Around(value = "bean(UserController) && execution( * Controller.UserController.addUser(..))&& args(user)", argNames = "proceedingJoinPoint,user")
    public ResponseModel addUserCheck(ProceedingJoinPoint proceedingJoinPoint, User user) throws Throwable {
        responseModel.setNull();
        userPage.setNull();
        userPage.setUsername(user.getUsername());
        boolean flagUserIsExist =! userService.userList(userPage).isEmpty();
        boolean flagUsernameLegal = user.getUsername() == null;
        boolean flagPasswordLegal = user.getPassword() == null;
        if (flagPasswordLegal) {
            responseModel.setCode(210);
            responseModel.setMsg("密码空");
        } else if (flagUsernameLegal) {
            responseModel.setCode(211);
            responseModel.setMsg("用户名空");
        } else if (flagUserIsExist) {
            responseModel.setCode(209);
            responseModel.setMsg("用户名存在");
        } else {
            responseModel.setCode(200);
            proceedingJoinPoint.proceed(new Object[]{user});
            responseModel.setMsg("添加成功");
        }
        return responseModel;
    }

    /*这个通知是对登录员的修改参数合法性做检验的*/
    @Around(value ="bean(UserController) && execution(* Controller.UserController.updateUser(..)) && args(user))",argNames = "proceedingJoinPoint,user")
    public ResponseModel updateUserCheck(ProceedingJoinPoint proceedingJoinPoint,User user) throws  Throwable
    {
        responseModel.setNull();
        boolean flagUserNotexist=userService.selectUserById(user.getUserId())==null;
        boolean flagPasswordLegal = user.getPassword() == null;
       if(flagUserNotexist){
           responseModel.setCode(212);
           responseModel.setMsg("用户不存在");
       }
        else if (flagPasswordLegal) {
        responseModel.setCode(213);
        responseModel.setMsg("密码不能为空");
        }
        else {
            responseModel.setCode(200);
            proceedingJoinPoint.proceed(new Object[]{user});
            responseModel.setMsg("修改成功");
        }
        return responseModel;
    }

    /*这个通知是对登录员删除做参数合法性验证的*/
    @Around(value = "bean(UserController) && execution(* Controller.UserController.deleteUser(..)) && args(userId))",argNames = "proceedingJoinPoint,userId")
    public  ResponseModel deleteUserCheck(ProceedingJoinPoint proceedingJoinPoint,long userId) throws  Throwable
    {   responseModel.setNull();
        boolean  flagUserIsexist=userService.selectUserById(userId)!=null;
        if (flagUserIsexist)
        {
            responseModel.setCode(200);
            proceedingJoinPoint.proceed(new Object[]{userId});
            responseModel.setMsg("删除成功");
        }
        else {
            responseModel.setCode(214);
            responseModel.setMsg("用户不存在");
        }
        return  responseModel;
    }

    /*这个通知是用来对登录的参数做合法性校验的*/
    @Around(value = " bean(UserController) && execution(* Controller.UserController.logon(..)) &&args(user)" ,argNames = "proceedingJoinPoint,user")
    public ResponseModel logonCheck(ProceedingJoinPoint proceedingJoinPoint ,User user) throws Throwable
    {   responseModel.setNull();

        boolean flagPwdIsEmpty=user.getPassword()==null;
        boolean flagUsernameIsEmpty=user.getUsername()==null;
        boolean flagUsernameNotRight=(userService.logonCheck(user))==null;
        if (flagUsernameIsEmpty)
        {
            responseModel.setCode(215);
            responseModel.setMsg("用户名空");
        }
        else if (flagUsernameNotRight)
        {
            responseModel.setCode(216);
            responseModel.setMsg("用户名不存在，请检查");
        }
        else if (flagPwdIsEmpty){
            responseModel.setCode(217);
            responseModel.setMsg("密码不能为空");
        }
        else
            {
              proceedingJoinPoint.proceed(new Object[]{user});
            }
            return responseModel;
    }
}
